package com.tj.shopping.service.impl;

import com.tj.shopping.entity.Goods;
import com.tj.shopping.mapper.GoodInfoMapper;
import com.tj.shopping.service.IGoodInfoServiceImpl;
import com.tj.shopping.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodInfoServiceImpl implements IGoodInfoServiceImpl {


    private GoodInfoMapper mapper;

    @Override
    public Map<String, Object> finds(Map<String, Object> map) {
        if (null!=map && null!=map.get("page") && null!=map.get("limit")){
            map.put("page",Integer.parseInt(map.get("page").toString()));
            map.put("limit",Integer.parseInt(map.get("limit").toString()));
        }
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(GoodInfoMapper.class);
        List<Goods> list=mapper.findGoods(map);
        Map<String,Object> m=new HashMap<>();
        if (null==list||list.isEmpty()){
            m.put("code",600);
            m.put("msg","暂未数据");
        }else{
            m.put("code",200);
            m.put("msg","成功");
            m.put("data",list);
        }
        MybatisUtil.close(session);
        return m;
    }


    @Override
    public Map<String, Object> searchGoods(Map<String, Object> map) {
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(GoodInfoMapper.class);
        List<Goods> list=mapper.searchGoods(map);
        int total=mapper.total(map);
        Map<String,Object> m=new HashMap<>();
        if (null==list||list.isEmpty()){
            m.put("code",600);
            m.put("msg","暂未数据");
        }else{
            m.put("code",200);
            m.put("msg","成功");
            m.put("data",list);
            m.put("count",total);
        }
        MybatisUtil.close(session);
        return m;
    }
}
